﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThereAreNoIrreplaceblePeople.Models;
using ThereAreNoIrreplaceblePeople.Mailing;

namespace ThereAreNoIrreplaceblePeople.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/
        [Authorize]
        public ActionResult Index()
        {

            return View(new SearchViewModel());
        }

        //
        // POST: /Search/

        [Authorize]
        [HttpPost]
        public ActionResult Index(SearchViewModel searchViewModel)
        {
            try
            {
                Database1Entities3 db = new Database1Entities3();
                List<User> Users = db.User.ToList();
                foreach (User u in Users)
                {
                    var subjects = UserModel.GetSubjects(u.ID);
                    if ((subjects.Any(s => s.CourseCode == long.Parse(searchViewModel.SelectedSubject))))
                    {
                        searchViewModel.Users.Add(u);
                    }
                }
                return View(searchViewModel);
            }
            catch
            {
                return View(searchViewModel);
            }
        }

       
        //
        // GET: /Search/SendRequest

        public ActionResult SendRequest(string receiver, string subjectName, DateTime dt, int numberOfHours)
        {
            Database1Entities3 db = new Database1Entities3();

            //create object request and sen email to the person

            Request r = new Request();

            r.Date = dt; 
            r.GUID = Guid.NewGuid().ToString();
            r.Message = "";
            r.ReceiverID = receiver;
            r.SenderID = User.Identity.Name;
            r.NumberOfHours = numberOfHours;
            r.RequestStatus = 0;


            List<Subject> Subjects = db.Subject.ToList();
            Subject currentSubject;
            currentSubject = Subjects.Find(s => s.CourseCode == int.Parse(subjectName));
            r.SubjectName = currentSubject.CourseName;
            //r.SubjectName = subjectName;

            db.Request.AddObject(r);

            db.SaveChanges();

            //List<User> users = db.User.ToList(); //??
            //List<Subject> subjects = db.Subject.ToList();
            //Subject currentSubject = subjects.Find(s => s.CourseCode == int.Parse(subjectName));

                List<string> receipients = new List<string>();
                receipients.Add(receiver);

                MailHelper.ManageEmails(User.Identity.Name, receipients, currentSubject.CourseName, "http://" + Request.Url.Authority + "/Request/Accept?id=" + r.RequestNumber + "&&guid=" + r.GUID, "http://" + Request.Url.Authority + "/Request/Reject?id=" + r.RequestNumber + "&&guid=" + r.GUID, r.Date.Value, r.NumberOfHours);
            

            return RedirectToAction("Index");
        }

        public ActionResult SendEmail()
        {
            try
            {
                List<string> receipients = new List<string>();
                receipients.AddRange(new[] { "marta.chojnacka8@gmail.com", "morawskimck@gmail.com", "selamdj@gmail.com" });

                MailHelper.ManageEmails("antek@autograf.pl", receipients, "", "", "", DateTime.Now, 0);

                ViewBag.Message = "Your e-mail has been sent";
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex;
            }

            return RedirectToAction("Index");
        }
     
    }
}
